package com.wlps.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlps.entity.Client;
import com.wlps.entity.Order;
import com.wlps.result.ResultPageVo;
import com.wlps.service.IClientService;
import com.wlps.service.function.AbstractController;
import com.wlps.utils.PageUtils;
import com.wlps.vo.ClientOrderVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *客户中心
 * </p>
 *
 * @author ZWYZY
 * @since 2020/7/31
 */
@Component
public class ClientCenter extends AbstractController {
    @Resource
    IClientService iClientService;
    @Override
    public boolean judgeHasCommodity(long id) {
        return false;
    }

    @Override
    public Order generateOrder(Client client, long id) {
        return null;
    }
    
    public long addClient(Client client){
        iClientService.save(client);
        return client.getId();
    }
    public List<Client> queryClientByRequireC(String queryType, String clientIdInfo){
        List<Client>list =new ArrayList<>();
        if("客户ID".equals(queryType)){
            long id=Long.parseLong(clientIdInfo);
            Client client = iClientService.getById(id);
            list.add(client);
        }
        else{
            List<Client> clients = iClientService.list(new LambdaQueryWrapper<Client>().likeRight(Client::getName, clientIdInfo));
            list.addAll(clients);
        }
        return list;
    }

    public ClientOrderVo queryClientDetail(long id){
        return iClientService.findClientDetail(id);
    }

    public ResultPageVo<Client> clientPageInfo(){
        List<Client> page = iClientService.list();
        return PageUtils.getPageInfo(page);
    }
    public boolean queryClientExit(long id){
        return iClientService.getById(id) == null;
    }
    public Client updateClient(Client client){
        if(queryClientExit(client.getId()))
            throw new RuntimeException("该用户不存在");
        iClientService.updateById(client);
        return client;
    }
    public boolean removeClient(long id){
        if(queryClientExit(id)){
            throw new RuntimeException("该用户不存在无法删除");
        }
        iClientService.removeById(id);
        return true;
    }

 }
